<?php
/**
 * This is the template for generating a service class file for CRUD feature.
 * The following variables are available in this template:
 * - $this: the CrudCode object
 */
?>
<?php echo "<?php\n"; ?>
/**
 * <?php echo $this->serviceClass."\n";?>
 * @author <?php echo $this->author."\n"?>
 * @version <?php echo $this->version?> <?php echo date('Y-m-d H:i:s')."\n"?>
 */
class <?php echo $this->serviceClass; ?> extends CService {

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function view($id) {
		return $this->loadModel($id);
	}

	/**
	 * Creates a new model.
	 */
	public function create($<?php echo $this->modelClass?> = null) {
		$model = new <?php echo $this->modelClass;?>;

		if($<?php echo $this->modelClass?>) {
			$model->attributes = $<?php echo $this->modelClass?>;
			if($model->save())
				return CError::none($this, '保存成功');
		}

		return $model;
	}

	/**
	 * Updates a particular model.
	 * @param integer $id the ID of the model to be updated
	 */
	public function update($id, $<?php echo $this->modelClass?> = null) {
		$model=$this->loadModel($id);

		if($<?php echo $this->modelClass?>) {
			$model->attributes = $<?php echo $this->modelClass?>;
			if($model->save())
				return CError::none($this, '保存成功');
		}

		return $model;
	}

	/**
	 * Deletes a particular model.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function delete($id) {
		if($this->loadModel($id)->delete()) {
			return CError::none($this, '删除成功');
		} else {
			return CError::error($this, '删除失败');
		}
	}

	/**
	 * Lists all models.
	 */
	public function index() {
		$dataProvider=new CActiveDataProvider('<?php echo $this->modelClass; ?>');
		return $dataProvider;
	}

	/**
	 * Manages all models.
	 */
	public function admin($<?php echo $this->modelClass?> = null) {
		$model = new <?php echo $this->modelClass; ?>('search');
		$model->unsetAttributes();  // clear any default values
		if($<?php echo $this->modelClass?>)
			$model->attributes = $<?php echo $this->modelClass?>;

		return $model;
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=<?php echo $this->modelClass; ?>::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

}
